package cn.dlc.com.user.dao.impl;

import cn.dlc.com.user.dao.UserDao;
import cn.dlc.com.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 邓立川
 * @date 2021/11/21 18:44
 * @desc 当前类的描述信息
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public User loadUserByUsername(String username) throws UsernameNotFoundException {

        String sql = "select username, password from t_user where username=?";

        List<User> users = jdbcTemplate.query(sql, new Object[]{username}, new BeanPropertyRowMapper<>(User.class));

        if(users != null && users.size() > 0) {
            return users.get(0);
        }
        return null;
    }
}
